<template>
  <div class="page_box">
    <div class="tips_box">
      <div class="tips_item" v-for="item in nodeType" :key="item.name">
        <div class="tips_cicle" :style="{ background: item.color }"></div>
        <div class="tips_name">{{ item.name }}</div>
      </div>
    </div>
    <div id="jsmind_container" ref="jsmind"></div>
  </div>
</template>

<script>
import "jsmind/style/jsmind.css"; // 这样引入的样式 是全局样式

import jsMind from "jsmind/js/jsmind.js";
export default {
  data() {
    return {
      nodeType: [
        {
          name: "正常状态-A",
          color: "#6fe360",
          expanded: true,
        },
            {
          name: "注意状态-B",
          color: "#fdf553",
          expanded: true,
        },
            {
          name: "异常状态-C",
          color: "#f19937",
          expanded: true,
        },
      ],
    };
  },

  methods: {
    init() {
      var options = {
        // options 将在下一章中详细介绍
        container: "jsmind_container", // [必选] 容器的ID，或者为容器的对象
        editable: true, // [可选] 是否启用编辑
        theme: "orange", // [可选] 主题
        view: {
          engine: "canvas", // 思维导图各节点之间线条的绘制引擎
          hmargin: 100, // 思维导图距容器外框的最小水平距离
          vmargin: 50, // 思维导图距容器外框的最小垂直距离
          line_width: 2, // 思维导图线条的粗细
          line_color: "#555", // 思维导图线条的颜色
          draggable: false, // 当容器不能完全容纳思维导图时，是否允许拖动画布代替鼠标滚动
          hide_scrollbars_when_draggable: false, // 当设置 draggable = true 时，是否隐藏滚动条
          node_overflow: "hidden", // 节点文本过长时的样式
        },
        layout: {
          hspace: 50, // 节点之间的水平间距
          vspace: 30, // 节点之间的垂直间距
          pspace: 13, // 节点与连接线之间的水平间距（用于容纳节点收缩/展开控制器）
          cousin_space: 0, // 相邻节点的子节点之间的额外的垂直间距
        },
      };
      var jm = new jsMind(options);

      const configData = {
        /* 元数据，定义思维导图的名称、作者、版本等信息 */
        meta: {
          name: "jsMind-demo-tree",
          author: "hizzgdev@163.com",
          version: "0.2",
        },
        /* 数据格式声明 */
        format: "node_tree",

        /* 数据内容 */
        data: {
          id: "root",
          topic: "监控系统",
          children: [
            {
              id: this.generateUUID(),
              topic: "1FB",
              direction: "left",

              "background-color": "red",
              "border-color": "#000",
              "border-width": "2px",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "2FB",
              direction: "left",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "3FB",
              direction: "left",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "4FB",
              direction: "left",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "5FB",
              direction: "right",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "6FB",
              direction: "right",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "7FB",
              direction: "right",
              expanded: true,
            },
            {
              id: this.generateUUID(),
              topic: "8FB",
              direction: "right",
              expanded: true,
            },
          ],
        },
      };

      jm.show(configData);
    },
    // 获取 uuid
    generateUUID() {
      let d = new Date().getTime();
      let uuid = "xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx".replace(
        /[xy]/g,
        function (c) {
          let r = (d + Math.random() * 16) % 16 | 0;
          d = Math.floor(d / 16);
          return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
        }
      );
      return uuid;
    },
  },
  mounted() {
    this.init();
  },
};
</script>

<style lang="scss" scoped>
.page_box {
  width: 100%;
  height: 700px;
  //   border: 1px solid #000;
  position: relative;

  #jsmind_container {
    width: 100%;
    height: 100%;
  }

  .tips_box {
    position: absolute;
    display: flex;
    height: 30px;
    right: 0;
    top: -35px;
    .tips_item {
      display: flex;
      align-items: center;
      margin-left: 20px;
      .tips_cicle {
        width: 20px;
        height: 20px;
        // border: 1px solid #000;
        border-radius: 50%;
        margin-right: 5px;
      }
      .tips_name {
        font-size: 18px;
      }
    }
  }
}
</style>
